<template>
  <div class="module card">
    <div class="module-header">
      <h2><el-icon><Folder /></el-icon> 学习资源</h2>
      <div class="tabs-container">
        <el-radio-group
            :model-value="activeTab"
            size="small"
            @change="handleTabChange"
        >
          <el-radio-button label="class">我的班级课程</el-radio-button>
          <el-radio-button label="subscription">我的订阅课程</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div class="module-body">
      <div v-if="filteredResources.length > 0" class="resources-grid">
        <div
            class="resource-item"
            v-for="resource in filteredResources"
            :key="resource.id"
            @click="handleResourceClick(resource)"
        >
          <div class="resource-icon">
            <el-icon v-if="resource.type === 'word'"><Document /></el-icon>
            <el-icon v-else-if="resource.type === 'ppt'"><DataAnalysis /></el-icon>
            <el-icon v-else-if="resource.type === 'pdf'"><DocumentCopy /></el-icon>
            <el-icon v-else-if="resource.type === 'video'"><VideoPlay /></el-icon>
            <el-icon v-else><Folder /></el-icon>
          </div>
          <div class="resource-details">
            <div class="resource-title">{{ resource.title }}</div>
            <div class="resource-update">更新: {{ resource.updatedAt }}</div>
          </div>
        </div>
      </div>
      <div v-else class="empty-state">
        <el-empty description="暂无资源" />
      </div>

      <!-- 添加更多按钮 -->
      <div class="more-resources" v-if="filteredResources.length > 0">
        <el-button
            type="primary"
            link
            @click="handleViewMore"
            class="more-button"
        >
          查看更多资源
          <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, defineProps, defineEmits } from 'vue';
import { ElMessage } from 'element-plus';
import {
  Folder, Document, DataAnalysis,
  DocumentCopy, VideoPlay, ArrowRight
} from '@element-plus/icons-vue';
import { useRouter } from "vue-router";

const router = useRouter();

const props = defineProps({
  resources: {
    type: Array,
    required: true,
    default: () => []
  },
  activeTab: {
    type: String,
    default: 'class'
  }
});

const emits = defineEmits(['update:activeTab', 'open-resource', 'view-more']);

// 计算过滤后的资源
const filteredResources = computed(() => {
  return props.resources.filter(
      resource => resource.category === props.activeTab
  );
});

// 处理选项卡变化
const handleTabChange = (value) => {
  emits('update:activeTab', value);
};

// 根据资源类型映射到对应的预览路由
const getPreviewRoute = (resourceType) => {
  const routeMap = {
    'pdf': '/student/fileViewerPDF',
    'excel': '/student/fileViewerExcel', // 注意：您的资源类型是 'ppt'，但路由是 'excel'？这里按路由配置处理，您可能需要调整
    'word': '/student/fileViewerWord',
    'video': '/student/fileViewerVideo',
  };
  // 默认情况，如果找不到映射，可以跳转到默认页面或给出提示
  return routeMap[resourceType] || '/student/resources'; // 跳转回资源中心
};

// 处理资源点击 - 跳转到对应类型的预览页面
const handleResourceClick = (resource) => {
  // 不再向上发射事件，而是直接路由跳转
  // emits('open-resource', resource); // 可以注释掉或删除这行

  const routePath = getPreviewRoute(resource.type);

  // 使用 router.push 跳转，并通过 query 传递资源信息
  router.push({
    path: routePath,
    query: {
      // 传递资源信息，例如ID、标题等，方便预览组件获取
      id: resource.id,
      title: resource.title,
      type: resource.type,
      // ... 其他需要传递的参数
    }
  });
};

// 处理查看更多
const handleViewMore = () => {
  router.push('/student/resources');
};
</script>



<style scoped>
.module {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  min-height: 200px;
  display: flex;
  flex-direction: column;
}

.module:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.module-header {
  position: relative;
  padding: 16px 20px;
  background: #1e62ec;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.module-header h2 {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.tabs-container {
  display: flex;
  align-items: center;
}

/* 选项卡样式 */
:deep(.el-radio-group) {
  margin-left: 15px;
}

:deep(.el-radio-button) {
  --el-radio-button-checked-bg-color: rgba(255, 255, 255, 0.2);
  --el-radio-button-checked-text-color: white;
  --el-radio-button-checked-border-color: transparent;
}

:deep(.el-radio-button__inner) {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: rgba(255, 255, 255, 0.8);
  border-radius: 4px;
  padding: 5px 15px;
  font-size: 13px;
  transition: all 0.3s;
}

:deep(.el-radio-button__inner:hover) {
  color: white;
  background: rgba(255, 255, 255, 0.15);
}

:deep(.el-radio-button:first-child .el-radio-button__inner) {
  border-radius: 4px 0 0 4px;
}

:deep(.el-radio-button:last-child .el-radio-button__inner) {
  border-radius: 0 4px 4px 0;
}

.module-body {
  padding: 20px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

/* 资源网格布局 */
.resources-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.resource-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-radius: 8px;
  background: #f9fafc;
  transition: all 0.3s;
  cursor: pointer;
  height: 100%;
  box-sizing: border-box;
}

.resource-item:hover {
  background: #e8f4ff;
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.resource-icon {
  width: 50px;
  height: 50px;
  background: #e6f1ff;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  flex-shrink: 0;
}

.resource-icon .el-icon {
  font-size: 28px;
  color: #1e62ec;
}

.resource-details {
  flex-grow: 1;
  min-width: 0;
}

.resource-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.resource-update {
  font-size: 13px;
  color: #909399;
}

/* 空状态样式 */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #a8a8a8;
  font-style: italic;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .module-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .tabs-container {
    width: 100%;
    justify-content: flex-end;
  }
}

@media (max-width: 992px) {
  .resources-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .resource-item {
    padding: 12px;
  }

  .resource-icon {
    width: 44px;
    height: 44px;
    margin-right: 12px;
  }

  .resource-icon .el-icon {
    font-size: 24px;
  }

  .resource-title {
    font-size: 15px;
  }

  .resource-update {
    font-size: 12px;
  }

  :deep(.el-radio-button__inner) {
    padding: 4px 12px;
    font-size: 12px;
  }
}
.more-resources {
  margin-top: 20px;
  padding-top: 15px;
  border-top: 1px solid #ebeef5;
  text-align: center;
}

.more-button {
  padding: 8px 20px;
  font-size: 14px;
  color: #1e62ec;
  transition: all 0.3s ease;
}

.more-button:hover {
  color: #1551c9;
  transform: translateX(4px);
}

.more-button .el-icon {
  margin-left: 6px;
  transition: transform 0.3s ease;
}

.more-button:hover .el-icon {
  transform: translateX(2px);
}

/* 响应式设计调整 */
@media (max-width: 768px) {
  .more-button {
    padding: 6px 16px;
    font-size: 13px;
  }
}
</style>
